home *** CD-ROM | disk | FTP | other *** search
- /* GetScreenDevice.c
- Copyright © 1989-1993 Denis G. Pelli
- HISTORY:
- 3/20/90 dgp make compatible with MPW C.
- 3/22/90 dgp changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
- from the baseAddr of the PixMap. This is a cleaner way to do it.
- 4/9/90 dgp eliminated #define for Mainscrn mispelling in Color.h
- 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
- the Mac IIci, IIsi, and LC.
- 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
- 8/24/91 dgp Made compatible with THINK C 5.0.
- 2/1/92 dgp Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
- 3/3/92 dgp In GetScreenDevice(), skip inactive screens.
- 8/20/92 dgp expanded comments of GetDeviceSlot(), noting that it works even with
- built-in video, e.g. on Mac IIci.
- 8/26/92 dgp GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
- GetScreenDevice() first checks for 8-bit QuickDraw().
- 9/10/92 dgp Actually implemented the 8/26 change instead of just changing the
- documentation. Oops.
- */
- #include "VideoToolbox.h"
-
- short int GetDeviceSlot(GDHandle device)
- // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
- // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
- // Zero is a legal slot for built-in video devices.
- {
- AuxDCEHandle myAuxDCEHandle;
-
- if(device == NULL) return -1;
- myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
- return ((**myAuxDCEHandle).dCtlSlot);
- }
-
- GDHandle GetScreenDevice(int n)
- // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
- // Returns NULL if request can't be satisfied.
- {
- GDHandle device;
- int i,error;
- long value;
-
- if(n<0)return NULL;
- error=Gestalt(gestaltQuickdrawVersion,&value);
- if(error || value<gestalt8BitQD)return NULL; // need 8-bit quickdraw
- if(n==0) return GetMainDevice();
- device=GetDeviceList();
- i=0;
- while(device!=NULL){
- if (TestDeviceAttribute(device,screenDevice)
- && !TestDeviceAttribute(device,mainScreen)
- && TestDeviceAttribute(device,screenActive)){
- i++;
- if(i==n)break;
- }
- device = GetNextDevice(device);
- }
- return device;
- }
-
- int GetScreenIndex(GDHandle device)
- // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
- {
- int i,error;
- long value;
-
- error=Gestalt(gestaltQuickdrawVersion,&value);
- if(error || value<gestalt8BitQD)return 0;
- if(device==NULL)return -1;
- for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
- return -1;
- }
-
- GDHandle GetWindowDevice(WindowPtr window)
- // Returns GDHandle of screen with largest intersection with the window's content.
- {
- Rect r,overlap;
- GDHandle device,dominantDevice=NULL;
- long area,greatestArea;
- WindowPtr oldWindow;
- int error;
- long value;
-
- if(window==NULL)return NULL;
- error=Gestalt(gestaltQuickdrawVersion,&value);
- if(error || value<gestalt8BitQD)return NULL; // need 8-bit quickdraw
- r=window->portRect;
- GetPort(&oldWindow);
- SetPort(window);
- LocalToGlobalRect(&r);
- SetPort(oldWindow);
- device=GetDeviceList();
- greatestArea=0;
- while(device!=NULL){
- if(TestDeviceAttribute(device,screenDevice)
- && TestDeviceAttribute(device,screenActive)){
- SectRect(&r,&(*device)->gdRect,&overlap);
- area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
- if(area>greatestArea){
- greatestArea=area;
- dominantDevice=device;
- }
- }
- device=GetNextDevice(device);
- }
- return dominantDevice;
- }
-
- GDHandle AddressToScreenDevice(Ptr address)
- // Returns a handle to the screen device containing pixels at the given address.
- // Returns NULL if request can't be satisfied.
- // The algorithm rounds addresses down to multiples of 16 MB and checks for equality.
- {
- GDHandle device;
- long mask;
- int error;
- long value;
-
- error=Gestalt(gestaltQuickdrawVersion,&value);
- if(error || value<gestalt8BitQD)return NULL; // need 8-bit quickdraw
- mask=~(0x1000000-1);
- device=GetDeviceList();
- while(device!=NULL) {
- if (TestDeviceAttribute(device,screenDevice) &&
- (((long)address&mask)==((long)(**(**device).gdPMap).baseAddr&mask)))
- break;
- device=GetNextDevice(device);
- }
- return device;
- }
-
- GDHandle SlotToScreenDevice(int n)
- // Returns a handle to the screen device in slot n.
- // Returns NULL if request can't be satisfied.
- {
- GDHandle device;
-
- device=GetDeviceList();
- while(device!=NULL) {
- if (TestDeviceAttribute(device,screenDevice) &&
- GetDeviceSlot(device)==n)
- break;
- device=GetNextDevice(device);
- }
- return device;
- }
-
- short int AddressToSlot(Ptr address)
- // Old routine. Doesn't work with built in video: Mac IIci, IIsi, LC.
- // Use AddressToScreenDevice() instead.
- // Returns the slot a given address is in. Returns zero if none.
- {
- unsigned long base;
- base=(unsigned long) address;
- if (0xf9000000 < base && base < 0xfeffffff)
- return (base/0x1000000)%16;
- return 0;
- }
-
- void LocalToGlobalRect(Rect *r)
- {
- Point pt={0,0};
-
- LocalToGlobal(&pt);
- OffsetRect(r,pt.h,pt.v);
- }
-
- void GlobalToLocalRect(Rect *r)
- {
- Point pt={0,0};
-
- GlobalToLocal(&pt);
- OffsetRect(r,pt.h,pt.v);
- }